package com.woniuxy.his.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.his.common.CommonUtils;
import com.woniuxy.his.common.Result;
import com.woniuxy.his.entity.Doctor;
import com.woniuxy.his.entity.User;
import com.woniuxy.his.mapper.DoctorMapper;
import com.woniuxy.his.mapper.UserMapper;
import com.woniuxy.his.service.ILoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

@Service
@Slf4j
public class LoginServiceImpl implements ILoginService {


    @Resource
    private DoctorMapper doctorMapper;

    @Resource
    private UserMapper userMapper;


    @Override
    public Result login(String username, String password) throws UnsupportedEncodingException, NoSuchAlgorithmException {

        String md5 = CommonUtils.md5(password);
        log.info("加密后为{}",md5);
        QueryWrapper<Doctor> wrapper = new QueryWrapper<>();
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        userQueryWrapper.eq("username",username);


        User user=userMapper.selectOne(userQueryWrapper);
        Doctor doctor= doctorMapper.selectOne(wrapper);

        if (null!=user){
            log.info("密码为{}",user.getPassword());
            log.info("找到用户名为{}的用户",username);
            if (user.getPassword().equals(md5)) {
                return Result.success("200","登录成功",user);
            }
            return Result.fail("400","密码错误",null);
        }
        return Result.fail("400","未找到该用户",null);
    }
}
